package chapter6.meine;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JApplet;
import javax.swing.JPanel;

import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;

public class PaintWithMouseInDiffColors extends JApplet{
	
//	public enum ColorPalette {
//		BLACK(0), RED(1), GREEN(2), BLUE(3), CYAN(4), MAGENTA(5), YELLOW(6);
//		private int colorValue;
//
//		ColorPalette(int c) {
//			colorValue = c;
//		}
//
//		public int getColorValue() {
//			return colorValue;
//		}
//		public String toString(){
//				return name(); 
//			}
//	}
	
	public class Panel extends JPanel implements MouseListener, MouseMotionListener, UndoableEditListener{
		
		int width, height;
		int colorSpacing;
		int prevX, prevY, lastX, lastY;
		boolean dragging;
		
		private int currentColor;
		
		private Graphics mainPanelDrawing;
		private Graphics[] lastPanel;
		private int num;
		
	      private final static int BLACK = 0,
                  RED = 1,     
                  GREEN = 2,   
                  BLUE = 3, 
                  CYAN = 4,   
                  MAGENTA = 5,
                  YELLOW = 6;
		
		public Panel (){
			super();
			addMouseListener(this);
			addMouseMotionListener(this);
			UndoManager undoManager = new UndoManager();
			setBackground(Color.white);				
		}
		
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			width = getWidth();
			height = getHeight();
			g.setColor(Color.gray); // fills the main area
			g.fillRect(0, 0, width, height);
			
			
			g.setColor(Color.white); // fills white area
			g.fillRect(3, 3, width-59, height-6);
			
			g.setColor(Color.white); // draws "Clear" rectangular
			g.fillRect(width-53, height-53, 50, 50);
			g.setColor(Color.black);
			Font f = new Font("Dialog",Font.BOLD, 12);
			g.setFont(f);
			g.drawString("Clear", width-43, height-23);
			
			colorSpacing = (height-3*9-50)/7; // draws 7 Color rectangulars
			g.setColor(Color.black); 
			g.fillRect(width-53, 0*colorSpacing+3*1, 50, colorSpacing);
			g.setColor(Color.red); 
			g.fillRect(width-53, 1*colorSpacing+3*2, 50, colorSpacing);
			g.setColor(Color.green); 
			g.fillRect(width-53, 2*colorSpacing+3*3, 50, colorSpacing);
			g.setColor(Color.blue); 
			g.fillRect(width-53, 3*colorSpacing+3*4, 50, colorSpacing);
			g.setColor(Color.cyan); 
			g.fillRect(width-53, 4*colorSpacing+3*5, 50, colorSpacing);
			g.setColor(Color.magenta); 
			g.fillRect(width-53, 5*colorSpacing+3*6, 50, colorSpacing);
			g.setColor(Color.yellow); 
			g.fillRect(width-53, 6*colorSpacing+3*7, 50, colorSpacing);
				
		}// end of paint() method
		
		public void setUpColor(){
			mainPanelDrawing = getGraphics();
			switch (currentColor) {
			case 0:
				mainPanelDrawing.setColor(Color.black);
				break;
			case 1:
				mainPanelDrawing.setColor(Color.red);
				break;
			case 2:
				mainPanelDrawing.setColor(Color.green);
				break;
			case 3:
				mainPanelDrawing.setColor(Color.blue);
				break;
			case 4:
				mainPanelDrawing.setColor(Color.cyan);
				break;
			case 5:
				mainPanelDrawing.setColor(Color.magenta);
				break;
			case 6:
				mainPanelDrawing.setColor(Color.yellow);
				break;
			default:
				break;
			}
			
		}
		public void changeColor(int y){
	         int newColor = y / colorSpacing;       // Which color number was clicked?
	         
	         if (newColor < 0 || newColor > 6)      // Make sure the color number is valid.
	            return;
			
	         currentColor = newColor;
	         
	         
	         
		}

		@Override
		public void mouseDragged(MouseEvent e) {
	        
	         if (dragging == false)
	            return;  // Nothing to do because the user isn't drawing.
			if (dragging == true){
			lastX = e.getX();
			lastY = e.getY();
			lastPanel[num] = mainPanelDrawing;
			mainPanelDrawing.drawLine(prevX, prevY, lastX, lastY);
			prevX = lastX;
			prevY = lastY;
			}
		}

		@Override
		public void mouseMoved(MouseEvent e) {}

		@Override
		public void mouseClicked(MouseEvent e) {}
			
		@Override
		public void mouseEntered(MouseEvent e) {}
		
		@Override
		public void mouseExited(MouseEvent e) {}

		@Override
		public void mousePressed(MouseEvent e) {
		       int x = e.getX();   // x-coordinate where the user clicked.
		       int y = e.getY();   // y-coordinate where the user clicked.         
		         if (dragging == true)  // Ignore mouse presses that occur
		            return;            //    when user is already drawing a curve.
		                                //    (This can happen if the user presses
		                                //    two mouse buttons at the same time.)
		         
		         if (x > width - 53) {
		               // User clicked to the right of the drawing area.
		               // This click is either on the clear button or
		               // on the color palette.
		            if (y > height - 53)
		            	repaint();       //  Clicked on "CLEAR button".
		            else
		               changeColor(y);  // Clicked on the color palette.
		         }
		         else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
		               // The user has clicked on the white drawing area.
		               // Start drawing a curve from the point (x,y).
		            prevX = x;
		            prevY = y;
		            dragging = true;
		            setUpColor();
		         }
		         
		      } // end mousePressed()

		@Override
		public void mouseReleased(MouseEvent e) {
		      if (dragging == false)
		            return;  // Nothing to do because the user isn't drawing.
		         dragging = false;
		         mainPanelDrawing.dispose();
		         mainPanelDrawing = null;
			
		}


		@Override
		public void undoableEditHappened(UndoableEditEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		
	}// end of class Panel
	
	 public void init() {	      
		  Panel content = new Panel();
		  setContentPane(content);
		  

	   }
	

} // end of class PaintWithMouseInDiffColors
